<?php namespace Phpcmf\Controllers\Member;


class Dizhi extends \Phpcmf\Home\Module
{
	public function index() {
		\Phpcmf\Service::V()->assign(\Phpcmf\Service::L('Seo')->get_seo_value($data, [
		    'meta_title' => '地址',
		    'meta_keywords' => isset($this->form['setting']['seo']['keywords']) && $this->form['setting']['seo']['keywords'] ? $this->form['setting']['seo']['keywords'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		    'meta_description' => isset($this->form['setting']['seo']['description']) && $this->form['setting']['seo']['description'] ? $this->form['setting']['seo']['description'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		]));
		\Phpcmf\Service::V()->assign([
		    'data' => $data,
		]);
		\Phpcmf\Service::V()->display("dizhi.html");
	}
	//列表
	public function list() {
		$uid = (int)\Phpcmf\Service::L('input')->get('uid');
		if (!$this->uid) {
		    $rt=[
		    	'code'=>0,
		        'msg'=>'没有登陆',
		    	'data'=>'',
		    ];
		    return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		} elseif (!$uid) {
		   
			$rt=[
				'code'=>0,
			    'msg'=>'uid参数不完整',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		// 获取帖子数据
		$table = '1_quan_dizhi';
		$dizhidata =\Phpcmf\Service::M()->table($table)->where('uid', $this->uid)->order_by('moren desc, id desc')->getAll();
		//->order('moren', 'asc') // 先按照field1升序排序  
		//->order('inputtime', 'desc') // 再按照field2降序排序->orderBy('moren ASC, id ASC')
		$rt=[
			'code'=>1,
		    'msg'=>'获取成功',
			'data'=>$dizhidata,
		];
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	//添加
	public function add() {
		
		if(IS_POST){
			//$xingming = \Phpcmf\Service::L('input')->post('xingming');
			// dr_debug('文件名称$_POST', $_POST);
			$xingming = dr_safe_replace($_POST['xingming']);
			$shoujihaoma = dr_safe_replace($_POST['shoujihaoma']);
			$dizhi = dr_safe_replace($_POST['dizhi']);
			
			if (!$this->uid) {
			    $rt=[
			    	'code'=>0,
			        'msg'=>'没有登陆',
			    	'data'=>'',
			    ];
			    return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			} elseif (!$xingming) {
			   
				$rt=[
					'code'=>0,
				    'msg'=>'姓名不能为空',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			elseif (!$shoujihaoma) {
			   
				$rt=[
					'code'=>0,
				    'msg'=>'手机号码不能为空',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			elseif (!$dizhi) {
			   
				$rt=[
					'code'=>0,
				    'msg'=>'地址不能为空',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}

			
			// 获取帖子数据
			$table = '1_quan_dizhi';
			
			$morendata = \Phpcmf\Service::M()->db->table($table)->where('moren', 1)->where('uid', $this->uid)->countAllResults();
			if($morendata > 0){
				$moren=0;
			} else {
				$moren=1;
			}
			//添加正表
			$datas = [
				'xingming'=> $xingming, //姓名
				'shoujihaoma'=> $shoujihaoma, //号码
				'moren'=> $moren,
				'uid'=> $this->uid, //用户
				'dizhi'=> $dizhi, //地址
				'inputtime'=>SYS_TIME,
			];
			\Phpcmf\Service::M()->db->table($table)->insert($datas);
			$rt=[
				'code'=>1,
			    'msg'=>'添加成功',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		
		$rt=[
			'code'=>0,
		    'msg'=>'参数错误',
			'data'=>'',
		];
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	//默认
	public function moren() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$table = '1_quan_dizhi';
		$data= \Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->where('id', $id)->get()->getRowArray();
		if(!$data){
			$rt=[
				'code'=>0,
			    'msg'=>'id参数错误',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		if(!$this->uid){
			$rt=[
				'code'=>0,
			    'msg'=>'请先没有登录',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		\Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->where('id', $id)->update([
		    'moren' => 1,
		]);
		//获取所有id
		//$dizhidata =\Phpcmf\Service::M()->table($table)->where('uid', $this->uid)->getAll();
		$dizhishuliang = \Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->countAllResults();
		if($dizhishuliang > 1){
			$dizhidata = \Phpcmf\Service::M()->table($table)
				->where('uid', $this->uid)  
				->select('id') // 选择id字段  
				->getAll();
			// 提取所有的ID  
			$ids = array_column($dizhidata, 'id');
			
			// 排除ID为3的记录
			$ids = array_filter($ids, function($ida) use ($id) {  
				return $ida != $id;  
			});
			$ids_string = '['.implode(',', $ids).']';
			\Phpcmf\Service::M()->db->table($table)
				->where('uid', $this->uid) // 根据uid字段进行过滤
				->whereIn('id', $ids) // 在id字段中匹配给定的ID列表  
				->update(['moren' => 0]); // 将moren字段更新为0
		} else {
			
		}

		//$ids = array_values($ids);  
		  
		// 打印过滤后的ID数组以进行调试  
		//print_r($ids);
		
		
		$rt=[
			'code'=>1,
		    'msg'=>'设置成功',
			'data'=>'',
		];
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	public function xiugai() {
		
		if(IS_POST){
			//$xingming = \Phpcmf\Service::L('input')->post('xingming');
			// dr_debug('文件名称$_POST', $_POST);
			$id = (int)\Phpcmf\Service::L('input')->get('id');
			$xingming = dr_safe_replace($_POST['xingming']);
			$shoujihaoma = dr_safe_replace($_POST['shoujihaoma']);
			$dizhi = dr_safe_replace($_POST['dizhi']);
			$table = '1_quan_dizhi';
			
			\Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->where('id', $id)->update([
			    'xingming' => $xingming,
				'shoujihaoma' => $shoujihaoma,
				'dizhi' => $dizhi,
			]);
			$data= \Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->where('id', $id)->get()->getRowArray();
			$rt=[
				'code'=>1,
			    'msg'=>'修改成功',
				'data'=>$data,
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		} else {
			$id = (int)\Phpcmf\Service::L('input')->get('id');
			$table = '1_quan_dizhi';
			$data= \Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->where('id', $id)->get()->getRowArray();
			if(!$data){
				$rt=[
					'code'=>0,
				    'msg'=>'id参数错误0',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			if(!$this->uid){
				$rt=[
					'code'=>0,
				    'msg'=>'请先没有登录',
					'data'=>'',
				];
				return $this->_json($rt['code'], $rt['msg'], $rt['data']);
			}
			
			$rt=[
				'code'=>1,
			    'msg'=>'获取成功',
				'data'=>$data,
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
	}
	public function gouwudizhi() {
		$uid = (int)\Phpcmf\Service::L('input')->get('uid');
		$table = '1_quan_dizhi';
		$data= \Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->where('moren', 1)->get()->getRowArray();
		if(!$data){
			$rt=[
				'code'=>0,
			    'msg'=>'请填写默认地址',
				'data'=>'',
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}else {
			$rt=[
				'code'=>1,
			    'msg'=>'默认地址',
				'data'=> $data,
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
		$rt=[
			'code'=>1,
		    'msg'=>'修改成功',
			'data'=>$data,
		];
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		
	}
}